using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace AssemblyCYG
{
    public class CLogFactory
    {
        private string projectLogFilePath = string.Empty;
        private static object protect = "多线程并发保护！";

        /**************************************************LogFile******
         函数功能：     构造函数
         参数：         projectName              产品名称        in        
         返回值：       无
         **********************************************************/

        public CLogFactory(string path,string projectName)
        {
            FileStream stream = null;

            this.projectLogFilePath = string.Format(@"{0}{1}_{2}.txt", path, projectName, DateTime.Now.ToString("yyyy_MM_dd"));
            if (Directory.Exists(path))
            {
                if (!File.Exists(projectLogFilePath))
                {
                    //创建Log文件
                    stream = File.Create(projectLogFilePath);
                }
                else
                {
                }
            }
            else
            {
                //创建根Log文件夹
                Directory.CreateDirectory(path);
                //创建Log文件
                stream = File.Create(projectLogFilePath);
            }

            if (stream != null)
                stream.Dispose();
        }

        /********************************************************
         函数功能：     录入日志信息
         参数：         log                日志信息            in        
         返回值：       true               录入成功，false       录入失败
         **********************************************************/

        public bool WriteLog(string log)
        {
            string path = "\\Log\\", projectName = "Log";
            FileStream stream = null;

            this.projectLogFilePath = string.Format(@"{0}{1}_{2}.txt", path, projectName, DateTime.Now.ToString("yyyy_MM_dd"));
            if (Directory.Exists(path))
            {
                if (!File.Exists(projectLogFilePath))
                {
                    //创建Log文件
                    stream = File.Create(projectLogFilePath);
                }
                else
                {
                }
            }
            else
            {
                //创建根Log文件夹
                Directory.CreateDirectory(path);
                //创建Log文件
                stream = File.Create(projectLogFilePath);
            }

            if (stream != null)
                stream.Dispose();
            bool returnResult = false;
            lock (protect)
            {
                try
                {
                    using (StreamWriter sw = File.AppendText(this.projectLogFilePath))
                    {
                        sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":     " + log);
                        sw.Close();
                        returnResult = true;
                    }
                }
                catch
                {

                }
            }
            return returnResult;
        }

        public bool WriteLogWithOutTime(string log)
        {
            bool returnResult = false;
            lock (protect)
            {
                try
                {
                    using (StreamWriter sw = File.AppendText(this.projectLogFilePath))
                    {
                        sw.WriteLine(log);
                        sw.Close();
                        returnResult = true;
                    }
                }
                catch
                {

                }
            }
            return returnResult;
        }
    }
}

